<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>仓库管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/resources/css/public.css" media="all" />
</head>
<body class="childrenBody">
<!--搜索条件开始-->
<div>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>查询条件</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" lay-filter="searchFrm" id="searchFrm" name="searchFrm" method="post">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">购买客户</label>
                <div class="layui-input-inline">
                    <select name="customerid" id="search_customerid">
                        <option value="0">请选择客户</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-inline">
                    <select name="goodsid" id="search_goodsid">
                        <option value="0">请选择商品</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">开始时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="startTime" readonly="readonly"  id="startTime" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">操作员</label>
                <div class="layui-input-inline">
                    <input type="text" name="operateperson"  autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-inline">
                    <input type="text" name="remark"  autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">结束时间</label>
                <div class="layui-input-inline">
                    <input type="text"  name="endTime" readonly="readonly"  id="endTime"  autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center">
            <div class="layui-inline">
                <button type="button" class="layui-btn layui-btn-normal  layui-icon layui-icon-search" lay-filter="doSearch" lay-submit="">查询</button>
                <button type="reset" class="layui-btn layui-icon layui-icon-refresh">重置</button>
            </div>
        </div>
    </form>
</div>
<!--搜索条件结束-->

<!--数据表格开始-->
<div>
    <table class="layui-hide" id="SalesTable" lay-filter="SalesTable"></table>
    <div id="SalesToolBar" style="display: none;">
        <button type="button" lay-event="addSales" class="layui-btn layui-btn-sm "><span class="layui-icon layui-icon-add-circle"></span>添加商品销售</button>
    </div>
    <div id="SalesRowBar" style="display: none">
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>删除</button>
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm "><span class="layui-icon layui-icon-set-sm"></span>更新</button>
        <button type="button" lay-event="back" class="layui-btn layui-btn-sm layui-btn-normal"><span class="layui-icon layui-icon-edit"></span>退货</button>
    </div>
</div>
<!--数据表格结束-->
<!--添加和修改的弹出层开始-->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv" >
    <form action="" method="post" class="layui-form layui-form-pane" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">客户</label>
                <div class="layui-input-inline">
                    <select name="customerid" id="customerid"   lay-filter="customerid">
                        <option value="0">请选择供客户</option>
                    </select>
                    <div class="mydiv" title="不可修改" style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"> </div>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-inline">
                    <select name="goodsid" id="goodsid" lay-filter="goodsid">
                        <option value="0">请选择商品</option>
                    </select>
                    <div class="mydiv" title="不可修改" style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"> </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">销售数量</label>
                    <div class="layui-input-inline">
                        <input type="hidden" name="id"><!--form.val(); id赋值 以便修改-->
                        <input type="text" class="layui-input"  name="number" lay-verify="required|checkbuynumber" placeholder="请输入销售数量">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">销售价格</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="saleprice" lay-verify="number" placeholder="请输入销售价格">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">支付类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="paytype" value="支付宝" title="支付宝">
                    <input type="radio" name="paytype" value="微信" title="微信">
                    <input type="radio" name="paytype" value="信用卡" title="信用卡">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">销售备注</label>
                <div class="layui-input-block">
                    <textarea  name="remark" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block" style="text-align: center;">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="doSubmit" id="doSubmit" ><span class="layui-icon layui-icon-add-1"></span>提交</button>
                    <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
                </div>
            </div>
        </div>
    </form>
</div>
<!--添加和修改的弹出层结束-->

<!-- 退货的弹出层  开始 -->
<div id="backGoodsDiv" style="display: none;padding: 5px">
    <form action="" method="post" class="layui-form layui-form-pane" id="dataBackFrm" lay-filter="dataBackFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">退货数量</label>
            <div class="layui-input-block">
                <input type="hidden" name="id"> <!-- 退货单单ID -->
                <input type="hidden" name="currentnumber" id="currentnumber"> <!-- 当前销售单的数量 -->
                <input type="text" class="layui-input"  name="number" lay-verify="required|checknumber" placeholder="请输入退货数量">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">退货备注</label>
            <div class="layui-input-block">
                <textarea  name="remark" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="doBackSubmit" id="doBackSubmit" ><span class="layui-icon layui-icon-add-1"></span>提交</button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </div>
    </form>
</div>
<!-- 退货的弹出层  结束 -->

<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script>
    layui.use(['jquery','layer','form','table','laydate'],function () {
        var form=layui.form;
        var $=layui.jquery;
        var layer=layui.layer;
        var table=layui.table;
        var laydate = layui.laydate;

        //渲染时间选择器
        laydate.render({
            elem:'#startTime',
            type:'datetime'
        });
        laydate.render({
            elem:'#endTime',
            type:'datetime'
        });

        var tableIndex = table.render({
            elem: '#SalesTable'
            ,url:'/sales/loadAllSales'
            ,toolbar: '#SalesToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,title: '商品销售数据表'
            ,height: 'full-220'
            ,page: true
            ,cols: [ [
                {field:'id', title:'销售ID',align:'center' ,width:'80'}
                ,{field:'customername', title:'客户',align:'center',width:'120'}
                ,{field:'goodsname', title:'商品名称',align:'center',width:'120'}
                ,{field:'size', title:'商品规格',align:'center',width:'120'}
                ,{field:'salestime', title:'销售时间',align:'center',width:'120'}
                ,{field:'number', title:'销售数量',align:'center',width:'100'}
                ,{field:'saleprice', title:'销售价格',align:'center',width:'100'}
                ,{field:'remark', title:'备注',align:'center',width:'120'}
                ,{field:'operateperson', title:'操作员',align:'center',width:'100'}
                ,{fixed: 'right', title:'操作', toolbar: '#SalesRowBar',align:'center',width:'350'}
            ] ]
            ,done:function(data,curr,count){
                //不是第一页如果返回的数据为0那么就返回上一页
                if(data.data.length==0&&curr!=1){
                    tableIndex.reload({
                        page:{
                            curr:curr-1
                        }
                    });
                }
            }
        });

        //加载查询条件客户的下拉列表
        $.get("/customer/loadAllCustomerForSelect",function(res){
            var data=res.data;
            var dom=$("#search_customerid");
            var html='<option value="0">请选择客户</option>'
            $.each(data,function(index,item){
                html+='<option value="'+item.id+'">'+item.customername+'</option>'
            });
            dom.html(html);
            form.render("select");
        })
        $.get("/goods/loadAllGoodsForSelect",function(res){
            var data=res.data;
            var dom=$("#search_goodsid");
            var html='<option value="0">请选择商品</option>'
            $.each(data,function(index,item){
                html+='<option value="'+item.id+'">'+item.goodsname+'-'+item.size+'['+item.providername+']</option>'
            });
            dom.html(html);
            form.render("select");
        })


        //模糊查询
        form.on('submit(doSearch)',function(data){
            var params = $("#searchFrm").serialize();
            tableIndex.reload({
                url: "/sales/loadAllSales?" + params,
                page:{
                    curr:1
                }
            })
            return false;
        });

        //头工具栏事件
        table.on('toolbar(SalesTable)', function(obj){
            switch(obj.event){
                case 'addSales':
                    openAddSales();
                    break;
            };
        });


        //监听行工具条的事件
        table.on("tool(SalesTable)",function(obj){
            var data = obj.data; //获得当前行数据
            switch(obj.event){
                case 'delete':
                    DeleteSales(data);
                    break;
                case 'update':
                    UpdateSales(data);
                    break;
                case 'back':
                    back(data);
                    break;

            };
        });


        var url;
        var mainIndex;

        //添加添加页面
        function openAddSales() {
            mainIndex = layer.open({
                type:1,
                title:'添加销售',
                content:$("#saveOrUpdateDiv"),
                area:['800px','550px'],
                success:function(index){
                    //清空表单数据
                    $("#dataFrm")[0].reset();
                    initCustomerSelect();
                    initGoodsSelect();
                    url="/sales/addSales";
                    //不禁用客户和商品的下拉框
                    $(".mydiv").hide();
                }
            });
        }

        //初始化下拉框
        function initCustomerSelect(customerid){
            //商品下拉框清空
            var dom=$("#goodsid");
            dom.html("");
            //加载查询条件客户的下拉列表
            $.get("/customer/loadAllCustomerForSelect",function(res){
                var data=res.data;
                var dom=$("#customerid");
                var html='<option value="0">请选择客户</option>'
                $.each(data,function(index,item){
                    html+='<option value="'+item.id+'">'+item.customername+'</option>'
                });
                dom.html(html);
                //如果customerid有值就反选  给下拉框赋值
                //修改时给下拉框赋值 要放在这里 要现有赋值html后再赋值val才有效
                if(customerid!=undefined){
                    dom.val(customerid);
                }
                form.render("select");
            });
        }

        //加载商品下拉列表
        function initGoodsSelect(goodsid){
            $.get("/goods/loadAllGoodsForSelect",function(res){
                var data=res.data;
                var dom=$("#goodsid");
                var html='<option value="0">请选择商品</option>'
                $.each(data,function(index,item){
                    html+='<option value="'+item.id+'">'+item.goodsname+'-'+item.size+'</option>'
                });
                dom.html(html);
                //如果goodsid有值就反选 给下拉框赋值
                //修改时给下拉框赋值 要放在这里 要现有赋值html后再赋值val才有效
                if(goodsid!=undefined){
                    dom.val(goodsid);
                }

                form.render("select");
            });
        }

        var goodshasnumber;
        //监听添加页面的供应商下拉框的改变事件
        form.on('select(goodsid)', function(data){
            var goodsid=data.value;
            //根据供应商ID查询拥有的商品
            $.post("/goods/loadGoodsByGoodId",{goodsid:goodsid},function (res) {
                goodshasnumber = res.number;
                console.log("商品剩余数量"+goodshasnumber);
            })
        });


        //更新页面
        function UpdateSales(data){
            mainIndex=layer.open({
                type:1,
                content:$("#saveOrUpdateDiv"),
                area:['800px','600px'],
                title:'修改商品销售',
                success:function(){
                    $("#dataFrm")[0].reset();
                    //装载新的数据
                    form.val("dataFrm",data);
                    initCustomerSelect(data.customerid);
                    initGoodsSelect(data.goodsid);
                    url="/sales/updateSales";
                    //禁用客户和商品的下拉框
                    $(".mydiv").show();

                }
            });
        }


        //保存
        form.on("submit(doSubmit)",function(obj){
            //序列化表单数据
            var params=$("#dataFrm").serialize();
            $.post(url,params,function(obj){
                layer.msg(obj.msg);
                //关闭弹出层
                layer.close(mainIndex);
                //刷新数据 表格
                if (obj.code ==200){
                    tableIndex.reload();
                }
            })
            return false;
        });




        //删除
        function  DeleteSales(data) {
            layer.confirm('你确定要删除这条数据吗?', {icon: 3, title:'提示'}, function(index){
                $.post("/sales/deleteSales",{id:data.id},function(res){
                    if(res.code==200){
                        tableIndex.reload();
                    }
                    layer.msg(res.msg);
                })
                layer.close(index);
            });
        }

        //自定义验证规则
        form.verify({
            checkbuynumber: function(value){
                var goodsid = $("#goodsid").val();
                if(parseInt(value)>goodshasnumber||parseInt(value)<1){
                    return '购买只能在【0】-【'+goodshasnumber+"】数量之间";
                }

            }
        });
        //打开退货的弹出层
        function back(data){
            mainIndex=layer.open({
                type:1,
                content:$("#backGoodsDiv"),
                area:['800px','500px'],
                title:'商品退货',
                success:function(){
                    $("#dataBackFrm")[0].reset();
                    //装载新的数据
                    form.val("dataBackFrm",{id:data.id,currentnumber:data.number});
                }
            });
        }

        //自定义验证规则
        form.verify({
            checknumber: function(value){
                var currentnumber=parseInt($("#currentnumber").val());
                if(parseInt(value)>currentnumber||parseInt(value)<1){
                    return '退货数量只能在【0】-【'+currentnumber+"】数量之间";
                }
            }
        });

        //退货提交
        form.on("submit(doBackSubmit)",function(data){
            $.post("/salesback/addSalesback",data.field,function(res){
                if(res.code==200){
                    tableIndex.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            })
            return false;
        });
    })
</script>
</body>
</html>